# -*- coding: UTF-8 -*-
"""
    @Project ：bao_game_backup
    @File ：read_config_create.py
    @Author ：lc
    @Date ：2021.12.31 19:06
"""
import configparser


class MyConfigParser(configparser.ConfigParser):
    """
    继承重写optionxform，去掉默认返回小写.lower()方法
    """

    def __init__(self):
        configparser.ConfigParser.__init__(self)

    def optionxform(self, optionstr):
        return optionstr


if __name__ == "__main__":
    config = MyConfigParser()
    # 读取配置文件
    config.read("example.ini", encoding="utf-8")
    # 返回标签名
    sections_list = config.sections()
    # 获取文件名创建文件
    file_name = config.options("file_name")
    py_file = open(f"{file_name[0]}.py", "w", encoding="utf-8")
    # 前置导入包语句
    import_sentence = "from selenium.webdriver.common.by import By\nfrom base.base_page import BasePage, BaseOperate\nfrom utils import select_box, UtilsDriver\n"
    py_file.writelines(import_sentence)
    # 创建页面类
    class_name = config.options("page_class_name")
    class_sentence = f"\n\n# 页面层\nclass {class_name[0]}({config['page_class_name'][class_name[0]]}):\n\tdef __init__(self):\n\t\tsuper().__init__()"
    py_file.writelines(class_sentence)
    # 创建元素对象定位方式
    att_create = config.options("page_att")
    for att in att_create:
        att_item = config["page_att"][att].split(",")
        att_sentence = f"\n\t\tself.{att} = By.{att_item[0]}, \"{att_item[1]}\""
        py_file.writelines(att_sentence)
    # 创建属性查找函数
    for att in att_create:
        find_sentence = f"\n\n\tdef find_{att}(self):\n\t\treturn self.get_element(self.{att})"
        py_file.writelines(find_sentence)
    # 创建操作层
    class_operate_name = config.options("operate_class_name")
    class_sentence = f"\n\n\n# 操作层\nclass {class_operate_name[0]}({config['operate_class_name'][class_operate_name[0]]}):\n\tdef __init__(self):\n\t\tself.page_obj = {class_name[0]}()"
    py_file.writelines(class_sentence)
    # 创建操作函数
    for att in att_create:
        att_item = config["page_att"][att].split(",")
        if att_item[2] == "click":
            fun_sentence = f"\n\n\tdef click_{att}(self):\n\t\tself.page_obj.find_{att}().click()"
            py_file.writelines(fun_sentence)
        if att_item[2] == "input":
            fun_sentence = f"\n\n\tdef input_{att}(self, {att}):\n\t\tself.input_text(self.page_obj.find_{att}(), {att})"
            py_file.writelines(fun_sentence)
        if att_item[2] == "select":
            fun_sentence = f"\n\n\tdef input_{att}(self, {att}):\n\t\tselect_box(UtilsDriver.get_web_driver(), self.page_obj.find_{att}(), {att})"
            py_file.writelines(fun_sentence)
        if att_item[2] == "text":
            fun_sentence = f"\n\n\tdef get_{att}_{att_item[2]}(self):\n\t\treturn self.page_obj.find_{att}().text"
            py_file.writelines(fun_sentence)
    # 创建业务层
    class_business_name = config.options("business_class_name")
    class_sentence = f"\n\n\n# 业务层\nclass {class_business_name[0]}:\n\tdef __init__(self):\n\t\tself.operate_obj = {class_operate_name[0]}()"
    py_file.writelines(class_sentence)
